/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uoc.tdp.client.informes;

import edu.uoc.tdp.beans.informes.Universitat;
import edu.uoc.tdp.beans.proves.Prova;
import edu.uoc.tdp.common.EOlympicsException;
import edu.uoc.tdp.common.EOlympicsUtils;
import edu.uoc.tdp.common.RemotInformesInterface;
import edu.uoc.tdp.common.TDSLanguageUtils;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 * Informe que permet als usuaris de perfil estudiant i als de perfil gestor, obtenir el medaller de l’event agrupat per universitats. S’accedeix des de l’opció de menú inclosa en el menú específic d’estadístiques o bé clicant el botó corresponent de la pantalla d’informes de l’estudiant / gestor.
 * @author joboni
 */
public class PantallaMedaller extends javax.swing.JFrame {
    
    private RemotInformesInterface remot;
    private DefaultTableModel dtm;
    private javax.swing.JTable jTable1;
    private javax.swing.JScrollPane jScrollPane2;
    /**
     * Creates new form PantallaMedaller
     */
    public PantallaMedaller() {
        try {
            initComponents();
            inicialitzarComponets();
        } catch (MalformedURLException e) {
            JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("informes.ERR_RMI_URL"), "Error", JOptionPane.ERROR_MESSAGE);
        } catch (NotBoundException e) {
            JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("informes.ERR_RMI_INIT"), "Error", JOptionPane.ERROR_MESSAGE);
        } catch (RemoteException e) {
            JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("informes.ERR_REMOT"), "Error", JOptionPane.ERROR_MESSAGE);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    /**
     * Metode per inicialitzar els components de la pantalla ( Literalas, mides, taules...)
     * @throws IOException
     * @throws NotBoundException
     * @throws RemoteException
     * @throws EOlympicsException 
     */
    private void inicialitzarComponets() throws IOException, NotBoundException, RemoteException, EOlympicsException{
        //Literals
        TDSLanguageUtils.setDefaultLanguage("edu/uoc/tdp/i18n/messages");
        //Recuperem les universitats
        ArrayList<Universitat> universitats = new ArrayList<Universitat>();
        universitats.add(null);
        for (Universitat u : Universitat.values()){
            universitats.add(u);
        }
        setTitle(TDSLanguageUtils.getMessage("informes.titol"));
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(universitats.toArray()));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(TDSLanguageUtils.getMessage("informes.filtreuni")));
        jLabel1.setText(TDSLanguageUtils.getMessage("informes.subtitolMedaller"));
        jLabel2.setText(TDSLanguageUtils.getMessage("informes.universitat"));
        jButton1.setText(TDSLanguageUtils.getMessage("informes.filtrar"));   
        //COnexió RMI
        Properties prop = new Properties();
        URL url = ClassLoader.getSystemResource("edu/uoc/tdp/properties/configuration.properties");
        prop.load(url.openStream());
        remot=(RemotInformesInterface)Naming.lookup(prop.getProperty("pathrmi_informes"));
        //TAULA
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new JTable(dtm);
        jTable1.setAutoCreateRowSorter(true);
        jScrollPane2.setViewportView(jTable1);
        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, Short.MAX_VALUE)
                .addContainerGap())
        );
        //ACtualitzem la taula
        actualitzarTaula(null);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    }
    /**
     * Método per actualitzar el contingut de la taula. Mostra els resultats a partir dels camps introduits per pantalla
     * @param universitat
     * @throws RemoteException
     * @throws EOlympicsException 
     */
    private void actualitzarTaula(Universitat universitat) throws RemoteException, EOlympicsException{
        //Recuperem les dades de la BBDD
        ArrayList<HashMap> resultats = remot.consultarMedaller(universitat);
        //Columnes
        String [] columnes = new String [] {
                TDSLanguageUtils.getMessage("informes.universitat"),
                TDSLanguageUtils.getMessage("informes.or"),
                TDSLanguageUtils.getMessage("informes.plata"),
                TDSLanguageUtils.getMessage("informes.bronze"),
                TDSLanguageUtils.getMessage("informes.total"),
            };
        Object [][] dades = new Object [resultats.size()][5];
        
        
        dtm =(new javax.swing.table.DefaultTableModel(
            dades,columnes
        ) {
            @Override
                public boolean isCellEditable(int rowIndex, int columnIndex) {
                return false;
                }
        });
        jTable1.setModel(dtm);
        jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
        int i = 0;
        //Introduim les dades recuperades a la taula
        for (HashMap resultat : resultats){
            dtm.setValueAt(resultat.get("UNIVERSITAT"), i, 0);
            dtm.setValueAt(resultat.get("OR"), i, 1);
            dtm.setValueAt(resultat.get("PLATA"), i, 2);
            dtm.setValueAt(resultat.get("BRONZE"), i, 3);
            dtm.setValueAt(resultat.get("TOTAL"), i, 4);
            i++;
            
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(450, 350));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Resultat proves finalitzades");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel1)
                .addContainerGap(30, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Universitat");

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel2)
                .addGap(38, 38, 38)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(95, 95, 95)
                .addComponent(jButton1)
                .addContainerGap(296, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        jPanel3.setPreferredSize(new java.awt.Dimension(0, 400));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 148, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 698, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /**
     * Métode que recuperar els camps de la pantalla un cop s'ha fet click en el butó buscar.
     * @param evt 
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            Universitat idUniversitat = null;
            
            if (jComboBox1.getSelectedItem()!=null){
                idUniversitat= ((Universitat)jComboBox1.getSelectedItem());
            }
            //Actualitzem la taula
            actualitzarTaula(idUniversitat);
        } catch (RemoteException ex) {
            JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("informes.ERR_REMOT"), "Error", JOptionPane.ERROR_MESSAGE);
        } catch (EOlympicsException ex) {
            JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("informes.ERROR"), "Error", JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    // End of variables declaration//GEN-END:variables
}



